#! /usr/bin/perl

#-----------------------------------------------------------
# This script is for INSURANCE2 final report generation.
#
# Customer may ask for either CSV or Excel format. The 
# format is based on customer request.
#
# The report can be delivered with Email or retrieved with FTP(S). 
# When using email or FTPS, encrytion is not necessary on local
# server, due to the use of secure email( or ftp) protocol. 
# But for regular FTP, the report will have to be encrypted 
# with client's gpg key.
#
# Database tables refered:
#   insurance0_output_t
#   insurance2_results_t
#
# Inputs:
# 	$cust_id = $ARGV[0]
#	$FTPDIR  = $ARGV[1] 
#	$BAKDIR  = $ARGV[2] 
#
# Exit code:
# 	0  -- OK
# 	1  -- Missing CMD line arg 
# 	3  -- database connection error 
# 	4  -- database table field not match 
#	5  -- output_id is not found
#-----------------------------------------------------------

use strict;
use PayorReport;
use TeamHealthReport;
use Net::Domain  qw(hostname);
use Getopt::Std;



sub usage() { 
	my $msg = qq(
	Usage:
	  $0 [-t] RECV_DATE  FTP_DIR  BAKUP_DIR  
	  	  -t		Database fields test, optional
		  RECV_DATE	mmddyy_hhmmss_xxxxxx where xxxxxx is custid padded with leading 0	
		  FTP_DIR	The specified ftp folder for customer
		  BAKUP_DIR	The backup directory
	\n);

	print STDERR $msg; 
}


#---------------------------------------------------
# start of the main script
#---------------------------------------------------

getopts('t');

if ( @ARGV < 3 ) { 
	usage(); exit 1;
}

my $basename= $ARGV[0];
my $FTPDIR  = $ARGV[1]; 
my $BAKDIR  = $ARGV[2]; 


	# run this script in the MLPROD TMP dir.
	chdir("$ENV{MLPROD_HOME}/tmp");

	#---------------------------------------------------
	# In srv3, PGHOST will be set by mlprodbatch daemon,
	# this block of code will not be excuted.
	#
	# In devlop box, this block of code will be executed
	#----------------------------------------------------
	if( ! exists $ENV{'PGHOST'} ) {
		my $host = hostname() ;
		my $alias = (gethostbyname($host))[1];
		my @aliases = split(/\s+/, $alias);
		my @dbhosts = grep(/^hdb-/, @aliases);
		if ( @dbhosts > 0 ) {
			$ENV{'PGHOST'} = shift @dbhosts;
		} else {
			$ENV{'PGHOST'} = shift @aliases;
		}
	}


	#-------------------------------------
	# Find out expected files  
	#  $ENV{MLPROD_HOME}/config/.mlprivate.cfg ;
	#------------------------------------
	my $g_dbschema = "payorintel";
	my $g_dbbasename="$ENV{USER}" || "mlprod" ;
	my $g_dbname=$g_dbbasename . "_db";
	my $g_dbuser=$g_dbschema . "_" . $g_dbbasename . "_u";

	my $report_obj;

    if($basename =~ /_000015$/ ) {	
        $report_obj = TeamHealthReport->new();
    } elsif($basename =~ /_000019$/ ) {		# Emcare
        $report_obj  = PayorReport->new( IGNORE => "cust_id,carrier_code," 
				. "extra1,extra2,extra3,extra4,extra5");
    } elsif($basename =~ /_000035$/ or $basename =~ /_000039$/) {         # EMP and EMA
        $report_obj  = PayorReport->new( IGNORE =>"extra1,extra2,extra3,extra4,extra5"); 
    } else {
        $report_obj  = PayorReport->new();
    }

    my $dbh = $report_obj->connect_db($g_dbname, $g_dbuser);
    if ( $dbh ) {
        $report_obj->create_and_deliver_report($basename , $FTPDIR , $BAKDIR);
    }

	exit 0;



